from rest_framework import serializers
from xj_users.models import User
from xj_users.models import Channel, Article, Comment
from cwd_articles.search import ArticleIndex

#频道序列化器
from xj_users.serialzier import UserSerialzier


class ChannelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ('id', 'name')

#文章列表序列器
class UserArticleUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')

class ArticleSerializerForList(serializers.ModelSerializer):
    user = UserSerialzier(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title","content","createtime","user","collected_users","collected","image","visits")

class ArticleUserSerializer(serializers.ModelSerializer):
    articles = UserArticleUserSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


# 文章创建序列化器
class ArticleCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('content', 'labels', 'title', 'channel', 'image')

    def create(self, validated_data):
        validated_data.update({'user': self.context.get('request').user})
        instance = super(ArticleCreateSerializer, self).create(validated_data)
        return instance


class ArticleSubCommentSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(read_only=True)
    subs = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = Comment
        fields = '__all__'


class ArticleCommentSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(read_only=True)
    subs = ArticleSubCommentSerializer(many=True, read_only=True)

    def validate(self, attrs):
        attrs['user'] = self.context['request'].user
        return attrs

    def create(self, validated_data):
        comment = super(ArticleCommentSerializer, self).create(validated_data)
        comment.article.comment_count += 1
        comment.save()
        return comment

    class Meta:
        model = Comment
        fields = '__all__'
        extra_kwargs = {
            'parent': {
                'required': False
            }
        }


class ArticleRetrieveSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(read_only=True)
    comments = ArticleCommentSerializer(many=True, read_only=True)

    class Meta:
        model = Article
        fields = '__all__'


class ArticleListSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(read_only=True)
    collected = serializers.SerializerMethodField()

    class Meta:
        model = Article
        exclude = ('updatetime', 'thumbup', 'comment_count', 'channel', 'labels')

    def get_collected(self, obj: Article):
        # 当前登录用户
        user = self.context.get('request').user
        if user.is_authenticated and (obj in user.collected_articles.all()):
            return True
        else:
            return False

class UserLableSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'labels')

# 搜索序列化器
from drf_haystack.serializers import HaystackSerializer


class ArticleIndexSerializer(HaystackSerializer):
    '''文章索引结果数据序列化器'''

    class Meta:
        index_classes = [ArticleIndex]
        fields = ('text', 'id', 'createtime', 'content', 'title')

